package com.hongkuncheng.cms.controller.admin;

import java.util.Calendar;
import java.util.Date;

import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Controller;
import com.hongkuncheng.cms.controller.BaseController;
import com.hongkuncheng.cms.helper.DateHelper;
import com.hongkuncheng.cms.model.Visitpv;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 访问量统计
 *
 * @author 洪坤成
 */
@Controller
@Transactional
@RequestMapping("admin/visitpv")
public class AvisitpvController extends BaseController {

    // 访问量统计
    @RequestMapping
    public String index() {
        checkAdminPermission("yunwei_webmaintain_visitstat");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        Object uniqueResult = factorySession.createCriteria(Visitpv.class)
                .setProjection(Projections.countDistinct("id")).uniqueResult();
        if (uniqueResult != null && Integer.parseInt(uniqueResult.toString()) > 0) {
            int day = request.getParameter("day") == null ? calendar.get(Calendar.DATE) :
                    Integer.parseInt(request.getParameter("day"));
            int month = request.getParameter("month") == null ? calendar.get(Calendar.MONTH) + 1 :
                    Integer.parseInt(request.getParameter("month"));
            int year = request.getParameter("year") == null ? calendar.get(Calendar.YEAR) :
                    Integer.parseInt(request.getParameter("year"));
            request.setAttribute("day", day);
            request.setAttribute("month", month);
            request.setAttribute("year", year);
            int visitByDay = Integer.parseInt(factorySession.createCriteria(Visitpv.class)
                    .add(Restrictions.sqlRestriction("date_format(visittime, '%Y')=?", year, StandardBasicTypes.INTEGER))
                    .add(Restrictions.sqlRestriction("date_format(visittime, '%m')=?", month, StandardBasicTypes.INTEGER))
                    .add(Restrictions.sqlRestriction("date_format(visittime, '%d')=?", day, StandardBasicTypes.INTEGER))
                    .setProjection(Projections.countDistinct("id")).uniqueResult().toString());
            request.setAttribute("visitByDay", visitByDay); // 日访问量
            int visitByMonth = Integer.parseInt(factorySession.createCriteria(Visitpv.class)
                    .add(Restrictions.sqlRestriction("date_format(visittime, '%Y')=?", year, StandardBasicTypes.INTEGER))
                    .add(Restrictions.sqlRestriction("date_format(visittime, '%m')=?", month, StandardBasicTypes.INTEGER))
                    .setProjection(Projections.countDistinct("id")).uniqueResult().toString());
            request.setAttribute("visitByMonth", visitByMonth); // 月访问量
            int visitByYear = Integer.parseInt(factorySession.createCriteria(Visitpv.class)
                    .add(Restrictions.sqlRestriction("date_format(visittime, '%Y')=?", year, StandardBasicTypes.INTEGER))
                    .setProjection(Projections.countDistinct("id")).uniqueResult().toString());
            request.setAttribute("visitByYear", visitByYear); // 年访问量
            request.setAttribute("visitTotal", Integer.parseInt(uniqueResult.toString())); // 总访问量
            StringBuilder columns = new StringBuilder(); // X坐标值
            StringBuilder columnsData = new StringBuilder(); // 访问量值
            int type = request.getParameter("type") == null ? 0 : Integer.parseInt(request.getParameter("type"));
            String typename = null;
            int maxHour = year == calendar.get(Calendar.YEAR) && month == calendar.get(Calendar.MONTH) + 1
                    && day == calendar.get(Calendar.DATE) ? date.getHours() : 23;
            int maxDay = year == calendar.get(Calendar.YEAR) && month == calendar.get(Calendar.MONTH) + 1 ?
                    calendar.get(Calendar.DATE) : DateHelper.daysInMonth(year, month);
            int maxMonth = year == calendar.get(Calendar.YEAR) ? calendar.get(Calendar.MONTH) + 1 : 12;
            int minYear = DateHelper.convertToDate(factorySession.createCriteria(Visitpv.class)
                    .setProjection(Projections.min("visittime")).uniqueResult().toString())
                    .getYear() + 1900;
            switch (type) {
                case 0: // 日访问量
                    for (int i = 0; i <= maxHour; i++) {
                        columns.append("," + i + "时");
                        columnsData.append("," + factorySession.createCriteria(Visitpv.class)
                                .add(Restrictions.sqlRestriction("date_format(visittime, '%Y')=?", year, StandardBasicTypes.INTEGER))
                                .add(Restrictions.sqlRestriction("date_format(visittime, '%m')=?", month, StandardBasicTypes.INTEGER))
                                .add(Restrictions.sqlRestriction("date_format(visittime, '%d')=?", day, StandardBasicTypes.INTEGER))
                                .add(Restrictions.sqlRestriction("date_format(visittime, '%H')>=?", i, StandardBasicTypes.INTEGER))
                                .add(Restrictions.sqlRestriction("date_format(visittime, '%H')<?", i + 1, StandardBasicTypes.INTEGER)).setProjection(Projections.countDistinct("id")).uniqueResult().toString());
                    }
                    typename = "今日";
                    break;
                case 1: // 月访问量
                    for (int i = 1; i <= maxDay; i++) {
                        columns.append("," + i + "日");
                        columnsData.append("," + factorySession.createCriteria(Visitpv.class)
                                .add(Restrictions.sqlRestriction("date_format(visittime, '%Y')=?", year, StandardBasicTypes.INTEGER))
                                .add(Restrictions.sqlRestriction("date_format(visittime, '%m')=?", month, StandardBasicTypes.INTEGER))
                                .add(Restrictions.sqlRestriction("date_format(visittime, '%d')>=?", i, StandardBasicTypes.INTEGER))
                                .add(Restrictions.sqlRestriction("date_format(visittime, '%d')<?", i + 1, StandardBasicTypes.INTEGER)).setProjection(Projections.countDistinct("id")).uniqueResult().toString());
                    }
                    typename = "本月";
                    break;
                case 2: // 年度访问量
                    for (int i = 1; i <= maxMonth; i++) {
                        columns.append("," + i + "月");
                        columnsData.append("," + factorySession.createCriteria(Visitpv.class)
                                .add(Restrictions.sqlRestriction("date_format(visittime, '%Y')=?", year, StandardBasicTypes.INTEGER))
                                .add(Restrictions.sqlRestriction("date_format(visittime, '%m')>=?", i, StandardBasicTypes.INTEGER))
                                .add(Restrictions.sqlRestriction("date_format(visittime, '%m')<?", i + 1, StandardBasicTypes.INTEGER)).setProjection(Projections.countDistinct("id")).uniqueResult().toString());
                    }
                    typename = "本年度";
                    break;
                case 3: // 全部访问量
                    for (int i = minYear; i <= calendar.get(Calendar.YEAR); i++) {
                        columns.append("," + i + "年");
                        columnsData.append("," + factorySession.createCriteria(Visitpv.class)
                                .add(Restrictions.sqlRestriction("date_format(visittime, '%Y')>=?", i, StandardBasicTypes.INTEGER))
                                .add(Restrictions.sqlRestriction("date_format(visittime, '%Y')<?", i + 1, StandardBasicTypes.INTEGER)).setProjection(Projections.countDistinct("id")).uniqueResult().toString());
                    }
                    typename = "全部";
                    break;
            }
            request.setAttribute("type", type);
            request.setAttribute("typename", typename);
            request.setAttribute("maxDay", maxDay);
            request.setAttribute("maxMonth", maxMonth);
            request.setAttribute("maxYear", calendar.get(Calendar.YEAR));
            request.setAttribute("minYear", minYear);
            request.setAttribute("columns", columns.deleteCharAt(0).toString());
            request.setAttribute("columnsData", columnsData.deleteCharAt(0).toString());
        }
        return view();
    }

}
